﻿#include "qt_hread_process.h"

//COM
COM m_com;



void callback(Mat mat_image,HObject hobject, void* p)
{

    QtThreadProcess* pCammerWidget = static_cast<QtThreadProcess*>(p);

//    pCammerWidget->m_qDisplayFrameQueue.push_back(hobject);

//    if(pCammerWidget->m_camera_index==3)
//        RotateImage(hobject,&hobject,90,"bilinear");

//    logger->info(to_string(pCammerWidget->m_camera_index) + "new image");
    if (pCammerWidget->m_proess_state == true)
    {



        int64 t1 = cv::getTickCount();
        ShowResult result;
		
        //result.m_image = image;
        result.m_detectin_result = pCammerWidget->m_function->detection(hobject, result.m_messgae);
        result.m_camera_index = pCammerWidget->m_camera_index;
        result.m_detection_index = pCammerWidget->m_detection_index;
        result.m_camera_name = pCammerWidget->m_camera_name;
        result.m_save_right = pCammerWidget->m_function->m_save_right;
        result.m_save_wrong = pCammerWidget->m_function->m_save_wrong;
        result.m_show_ratio =  pCammerWidget->m_show_ratio;


        Mat mat_image = himageToMat(hobject);

		if (result.m_detectin_result == true)
		{
			pCammerWidget->m_function->m_save_ok_vector_mat.push_back(mat_image.clone());
			if (pCammerWidget->m_function->m_save_ok_vector_mat.size() > 20)
			{
				pCammerWidget->m_function->m_save_ok_vector_mat.erase(pCammerWidget->m_function->m_save_ok_vector_mat.begin());
			}
		}
		else
        {
            pCammerWidget->m_function->m_save_ok_vector_mat.push_back(mat_image.clone());
            if (pCammerWidget->m_function->m_save_ok_vector_mat.size() > 20)
            {
                pCammerWidget->m_function->m_save_ok_vector_mat.erase(pCammerWidget->m_function->m_save_ok_vector_mat.begin());
            }

			pCammerWidget->m_function->m_save_ng_vector_mat.push_back(mat_image.clone());
			if (pCammerWidget->m_function->m_save_ng_vector_mat.size() > 20)
			{
				pCammerWidget->m_function->m_save_ng_vector_mat.erase(pCammerWidget->m_function->m_save_ng_vector_mat.begin());
			}
		}
		
        if(result.m_detectin_result )
        {
            pCammerWidget->m_ok_count++;
            m_com.sendCom(pCammerWidget->m_camera_index,3);

        }
        else
        {
            pCammerWidget->m_ng_count++;
            m_com.sendCom(pCammerWidget->m_camera_index,4);
            logger->info(pCammerWidget->m_camera_name.toStdString()+ "NG");
        }
        result.m_ok_count = pCammerWidget->m_ok_count;
        result.m_ng_count = pCammerWidget->m_ng_count;
        int64 t2 = cv::getTickCount();

        pCammerWidget->m_time_use = (t2 - t1) * 1000 / getTickFrequency();
        result.m_time_use = pCammerWidget->m_time_use;

        logger->info("camera" + std::to_string(pCammerWidget->m_camera_index)+ "检测时间" + std::to_string(pCammerWidget->m_time_use));

        result.m_hobject = hobject;

        result.m_frame_rate = pCammerWidget->m_frame_rate;


        if(pCammerWidget->m_delaye_number==0)
        {
            emit pCammerWidget->sendDetectionResult(result);
        }
        else
        {
            pCammerWidget->vector_showresult.push_back(result);
            if(pCammerWidget->vector_showresult.size()>=pCammerWidget->m_delaye_number)
            {
                ShowResult showresult;
                pCammerWidget->vector_showresult.get(showresult);
                emit pCammerWidget->sendDetectionResult(showresult);
            }
        }


        pCammerWidget->m_detection_index++;

    }
    if (pCammerWidget->m_only_show_state != false)
    {


            emit pCammerWidget->sendShowImage(mat_image, pCammerWidget->m_camera_index, pCammerWidget->m_image_index);
    }

    pCammerWidget->m_image_index++;

}

QtThreadProcess::QtThreadProcess(QObject* parent) 
{
    m_proess_state = false;
    m_only_show_state = false;

    qRegisterMetaType<Mat>("Mat");
    //对于自定义的结构体参数，信号槽无法识别参数，导致信号槽连接不起作用。
    //所以需要注册结构体参数。在结构体中声明结束的地方加上结构体注册。
    qRegisterMetaType<ShowResult>("ShowResult");

//    //存图线程开始
//    m_save_image_thread = new SaveImageThread();
//    m_save_image_thread->run();

	m_timer_frame_rate = new QTimer(this);
	connect(m_timer_frame_rate, &QTimer::timeout, this, [=]() {
		m_frame_rate = m_image_index - m_image_index_last;
		m_image_index_last = m_image_index;
	});
	m_timer_frame_rate->start(1000);//固定频率读取离线测试图像
	
}

void QtThreadProcess::run()
{
//    while (1)
//        {
//            HObject hobject;
//            if (false == m_qDisplayFrameQueue.get(hobject))
//            {
//                Sleep(1);
//                continue;
//            }


//    if (m_proess_state == true)
//    {
//        int64 t1 = cv::getTickCount();

////        cameraFinish(m_io_camera,m_io_finish,m_io_result);

////            Sleep(2);

//        ShowResult result;

//        //result.m_image = image;
//        result.m_detectin_result = m_function->detection(hobject, result.m_messgae);
//        result.m_camera_index = m_camera_index;
//        result.m_detection_index = m_detection_index;
//        result.m_camera_name = m_camera_name;
//        result.m_save_right = m_function->m_save_right;
//        result.m_save_wrong = m_function->m_save_wrong;
//        result.m_show_ratio =  m_show_ratio;


//        if(result.m_detectin_result )
//        {
////            emit pCammerWidget->sendCom(pCammerWidget->m_io_result,2);
////            emit pCammerWidget->sendCom(pCammerWidget->m_camera_index,3);
////            sendCom(m_camera_index,3);

//        }
//        else
//        {
////            emit pCammerWidget->sendCom(pCammerWidget->m_io_result,2);
////            emit pCammerWidget->sendCom(pCammerWidget->m_camera_index,4);

////            sendCom(m_camera_index,4);
//        }

//        if(result.m_detectin_result )
//        {
//            m_ok_count++;
//            m_com.sendCom(m_camera_index,3);
////            comResult(m_io_result,1,m_io_finish);
//        }
//        else
//        {
//            m_ng_count++;
//            m_com.sendCom(m_camera_index,4);
////            comResult(m_io_result,0,m_io_finish);
//        }
//        result.m_ok_count = m_ok_count;
//        result.m_ng_count = m_ng_count;
//        int64 t2 = cv::getTickCount();

//        m_time_use = (t2 - t1) * 1000 / getTickFrequency();
//        result.m_time_use = m_time_use;


//        result.m_hobject = hobject;

//        result.m_frame_rate =m_frame_rate;


//        if(m_delaye_number==0)
//        {
//            emit sendDetectionResult(result);
//        }
//        else
//        {
//            vector_showresult.push_back(result);
//            if(vector_showresult.size()>=m_delaye_number)
//            {
//                ShowResult showresult;
//                vector_showresult.get(showresult);
//                emit sendDetectionResult(showresult);
//            }
//        }


//        m_detection_index++;

//    }
//    if (m_only_show_state != false)
//    {

//            Mat image = himageToMat(hobject);
//            emit sendShowImage(image,m_camera_index, m_image_index);
//    }
//    }
}
